#coding:utf-8
"""Copyright (c) 2022 God Forever
Maze Generator is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details."""
import contextlib
with contextlib.redirect_stdout(None):
    import pygame
import random
import sys
import time
from tkinter.filedialog import *
while True:
    a = int(input("列数："))
    b = int(input("行数："))
    l = int(input("大小："))
    saveit = input("是否保存：")
    print("生成中...")
    e = time.time()
    alist = []
    aa = 0
    need = []
    for j in range(2 * a + 1):
        if aa == 0:
            aa = 1
            alistone = []
            for i in range(2 * b + 1):
                alistone.append(1)
            alist.append(alistone)
        else:
            aa = 0
            alistone = []
            bb = 0
            for i in range(2 * b + 1):
                if bb == 0:
                    bb = 1
                    alistone.append(1)
                else:
                    bb = 0
                    need.append((j, i))
                    alistone.append(0)
            alist.append(alistone)
    alist[0][1] = 0
    alist[-1][-2] = 0
    x = 1
    y = 1
    need.remove((1, 1))
    listing = []
    while len(need) > 0:
        aroundit = []
        try:
            if x - 2 < 0:
                print(1 + "1")
            alist[x - 2][y] = 0
            if (x - 2, y) in need:
                aroundit.append("alist[x-1][y],x=(0,x-2)")
        except:
            while False:
                print()
        try:
            alist[x + 2][y] = 0
            if (x + 2, y) in need:
                aroundit.append("alist[x+1][y],x=(0,x+2)")
        except:
            while False:
                print()
        try:
            alist[x][y + 2] = 0
            if (x, y + 2) in need:
                aroundit.append("alist[x][y+1],y=(0,y+2)")
        except:
            while False:
                print()
        try:
            if y - 2 < 0:
                print(1 + "1")
            alist[x][y - 2] = 0
            if (x, y - 2) in need:
                aroundit.append("alist[x][y-1],y=(0,y-2)")
        except:
            while False:
                print()
        if len(aroundit) > 0:
            listing.append((x, y))
            exec(random.choice(aroundit))
            need.remove((x, y))
        else:
            x, y = listing[-1]
            listing.pop()
    x2 = 0
    y2 = 1
    listing2 = []
    while not (alist[-1][-2] == 2):
        alist[x2][y2] = 3
        around2 = []
        try:

            if alist[x2 + 1][y2] == 0:
                around2.append("x2=x2+1")
        except:
            while False:
                print()
        try:

            if alist[x2][y2 + 1] == 0:
                around2.append("y2=y2+1")
        except:
            while False:
                print()
        try:
            if y2 - 1 < 0:
                print(1 + "1")
            if alist[x2][y2 - 1] == 0:
                around2.append("y2=y2-1")
        except:
            while False:
                print()
        try:
            if x2 - 1 < 0:
                print(1 + "1")

            if alist[x2 - 1][y2] == 0:
                around2.append("x2=x2-1")
        except:
            while False:
                print()
        if len(around2) > 0:
            listing2.append((x2, y2))
            exec(around2[0])
        else:
            alist[x2][y2] = 2
            x2, y2 = listing2[-1]
            listing2.pop()
    alist[-1][-2] = 3
    for i in range(len(alist)):
        for j in range(len(alist[0])):
            if alist[i][j] == 2:
                alist[i][j] = 0
    print("完成！用时{}秒".format(time.time() - e))
    if saveit == "1":
        ccc = askdirectory()
        h = ""
        bbbbb = 1
        while True:
            try:
                open("{}/{}×{}迷宫{}.png".format(ccc, a, b, h), "r")
                open("{}/{}×{}迷宫（正确线路）{}.png".format(ccc, a, b, h), "r")
                h = "({})".format(bbbbb)
            except:
                break
            bbbbb += 1
    pygame.init()
    icon = pygame.image.load("迷宫.png")
    pygame.display.set_icon(icon)
    screen = pygame.display.Info()
    screen = pygame.display.set_mode((l * (2 * a + 1), l * (2 * b + 1)))
    pygame.display.set_caption('迷宫')
    screen.fill("white")
    if saveit == "1":
        c = pygame.Surface((l, l), flags=pygame.HWSURFACE)
        c.fill(color='white')
        d = pygame.Surface((l, l), flags=pygame.HWSURFACE)
        d.fill(color='black')
        f = pygame.Surface((l, l), flags=pygame.HWSURFACE)
        f.fill(color='white')
        for i in range(2 * a + 1):
            for j in range(2 * b + 1):
                if alist[i][j] == 0:
                    screen.blit(c, (i * l, j * l))
                elif alist[i][j] == 1:
                    screen.blit(d, (i * l, j * l))
                else:
                    screen.blit(f, (i * l, j * l))
        pygame.image.save(screen, "{}/{}×{}迷宫{}.png".format(ccc, a, b, h))
        c = pygame.Surface((l, l), flags=pygame.HWSURFACE)
        c.fill(color='white')
        d = pygame.Surface((l, l), flags=pygame.HWSURFACE)
        d.fill(color='black')
        f = pygame.Surface((l, l), flags=pygame.HWSURFACE)
        f.fill(color='red')
        for i in range(2 * a + 1):
            for j in range(2 * b + 1):
                if alist[i][j] == 0:
                    screen.blit(c, (i * l, j * l))
                elif alist[i][j] == 1:
                    screen.blit(d, (i * l, j * l))
                else:
                    screen.blit(f, (i * l, j * l))
        pygame.image.save(screen, "{}/{}×{}迷宫（正确线路）{}.png".format(ccc, a, b, h))
    c = pygame.Surface((l, l), flags=pygame.HWSURFACE)
    c.fill(color='white')
    d = pygame.Surface((l, l), flags=pygame.HWSURFACE)
    d.fill(color='black')
    f = pygame.Surface((l, l), flags=pygame.HWSURFACE)
    f.fill(color='white')
    for i in range(2 * a + 1):
        for j in range(2 * b + 1):
            if alist[i][j] == 0:
                screen.blit(c, (i * l, j * l))
            elif alist[i][j] == 1:
                screen.blit(d, (i * l, j * l))
            else:
                screen.blit(f, (i * l, j * l))
    pygame.display.flip()
    aaaaaaa = 0
    kkk=0
    while True:
        if kkk==1:
            break
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                print()
                kkk=1
            if event.type == pygame.MOUSEBUTTONDOWN:
                if aaaaaaa == 1:
                    aaaaaaa = 0
                    c = pygame.Surface((l, l), flags=pygame.HWSURFACE)
                    c.fill(color='white')
                    d = pygame.Surface((l, l), flags=pygame.HWSURFACE)
                    d.fill(color='black')
                    f = pygame.Surface((l, l), flags=pygame.HWSURFACE)
                    f.fill(color='white')
                    for i in range(2 * a + 1):
                        for j in range(2 * b + 1):
                            if alist[i][j] == 0:
                                screen.blit(c, (i * l, j * l))
                            elif alist[i][j] == 1:
                                screen.blit(d, (i * l, j * l))
                            else:
                                screen.blit(f, (i * l, j * l))
                    pygame.display.flip()
                else:
                    aaaaaaa = 1
                    c = pygame.Surface((l, l), flags=pygame.HWSURFACE)
                    c.fill(color='white')
                    d = pygame.Surface((l, l), flags=pygame.HWSURFACE)
                    d.fill(color='black')
                    f = pygame.Surface((l, l), flags=pygame.HWSURFACE)
                    f.fill(color='red')
                    for i in range(2 * a + 1):
                        for j in range(2 * b + 1):
                            if alist[i][j] == 0:
                                screen.blit(c, (i * l, j * l))
                            elif alist[i][j] == 1:
                                screen.blit(d, (i * l, j * l))
                            else:
                                screen.blit(f, (i * l, j * l))
                    pygame.display.flip()